package com.kakarote.hrm.controller;

import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.hrm.entity.BO.QueryWorkbenchEmployeeListBO;
import com.kakarote.hrm.entity.PO.HrmEmployeeData;
import com.kakarote.hrm.entity.VO.EmployeeMiniInfo;
import com.kakarote.hrm.entity.VO.InformationFieldVO;
import com.kakarote.hrm.entity.VO.PersonalInformationVO;
import com.kakarote.hrm.service.IHrmEmployeeDataService;
import com.kakarote.hrm.service.IHrmEmployeeService;
import com.kakarote.hrm.service.IWxMiniEmpService;
import io.seata.common.util.CollectionUtils;
import io.seata.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 微信小程序员工管理
 * </p>
 *
 * @author huangmingbo
 * @since 2020-05-12
 */
@RestController
@RequestMapping("/wxmini/emp")
@Api(tags = "微信小程序员工管理")
public class WxMiniEmpController {

    @Autowired
    private IWxMiniEmpService wxMiniEmpService;

    @Autowired
    private IHrmEmployeeService employeeService;

    @Autowired
    private IHrmEmployeeDataService employeeDataService;

    @GetMapping("/getOrgEmpNums/{qId}/{kind}/{type}")
    @ApiOperation("查询机构统计")
    public Result getOrgEmpNums(@PathVariable("qId") Long qId , @PathVariable("kind") String kind , @PathVariable("type") Integer type) {
        return wxMiniEmpService.getOrgEmpNums(qId, kind, type);
    }

    @GetMapping("/getOrgSectorEmpNums/{orgSector}/{kind}/{type}")
    @ApiOperation("查询机构统计")
    public Result getOrgSectorEmpNums(@PathVariable("orgSector") Integer orgSector , @PathVariable("kind") String kind, @PathVariable("type") Integer type) {
        return wxMiniEmpService.getOrgSectorEmpNums(orgSector, kind, type);
    }

    /**
     * 仪表盘统计
     * @param deptId 部门编号（默认为0）
     * @param totalType 统计方式（WorkbenchTotalTypeEnum）
     * @param type 统计类型(3-所有人数、1-报表企业、2-报表及托管)
     * @param dimension 维度(1-人员、2-机构)
     * @return
     */
    @GetMapping("/workbenchTotal/{deptId}/{totalType}/{type}/{dimension}")
    @ApiOperation("仪表盘统计")
    public Result workbenchTotal(@PathVariable("deptId") Long deptId , @PathVariable("totalType") String totalType, @PathVariable("type") Integer type, @PathVariable("dimension") Integer dimension) {
        return wxMiniEmpService.workbenchTotal(deptId, totalType, type, dimension);
    }

    @PostMapping("/queryWorkbenchEmployeeList")
    @ApiOperation("仪表盘分页查询员工列表")
    public Result<BasePage<Map<String, Object>>> queryWorkbenchEmployeeList(@RequestBody QueryWorkbenchEmployeeListBO employeeListBO) {
        return Result.ok(wxMiniEmpService.queryWorkbenchPageEmployeeList(employeeListBO));
    }


    @GetMapping("/getOrgChilds/{deptId}")
    @ApiOperation("查询机构及员工人数")
    public Result getDeptChild(@PathVariable("deptId") Long deptId) {
        return wxMiniEmpService.getDeptChild(deptId);
    }


    @GetMapping("/getEmpListByOrg/{deptId}")
    @ApiOperation("查询员工列表")
    public Result getEmpListByOrg(@PathVariable("deptId") Long deptId) {
        return wxMiniEmpService.getEmpListByOrg(deptId);
    }

    @GetMapping("/personalInformation/{employeeId}")
    @ApiOperation("个人基本信息")
    public Result<PersonalInformationVO> personalInformation(@PathVariable("employeeId") Long employeeId) {
        PersonalInformationVO personalInformationVO = employeeService.personalInformation(employeeId);

        List<InformationFieldVO> information = personalInformationVO.getInformation();
        InformationFieldVO vo = new InformationFieldVO();

        List<HrmEmployeeData> list = employeeDataService.lambdaQuery()
                .eq(HrmEmployeeData::getEmployeeId, employeeId)
                .eq(HrmEmployeeData::getFieldName, "job_level")
                .list();
        if (CollectionUtils.isNotEmpty(list)) {
            HrmEmployeeData data = list.stream()
                    .filter(emp -> emp.getEmployeeId() != null && StringUtils.isNotEmpty(emp.getFieldValue()))
                    .sorted(Comparator.comparing(HrmEmployeeData::getCreateTime))
                    .findFirst().get();
            BeanUtils.copyProperties(data, vo);
            vo.setFieldValue(data.getFieldValue());
        }
        vo.setName("职位层级");
        information.add(vo);
        return Result.ok(personalInformationVO);
    }

    @GetMapping("/getEmp")
    @ApiOperation("个人基本信息")
    public List<EmployeeMiniInfo> getEmpList(EmployeeMiniInfo info) {

        return wxMiniEmpService.getEmpList(info);
    }
}

